   // ****************************************************** 
   //
   // Copyright 2011 OpenAjax Alliance
   //
   // Licensed under the Apache License, Version 2.0 (the "License");
   // you may not use this file except in compliance with the License.
   // You may obtain a copy of the License at
   //
   //    http://www.apache.org/licenses/LICENSE-2.0
   //
   // Unless required by applicable law or agreed to in writing, software
   // distributed under the License is distributed on an "AS IS" BASIS,
   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   // See the License for the specific language governing permissions and
   // limitations under the License.
   //
   // ****************************************************** 

var EXPORTED_SYMBOLS = ["OAA_CI_UTILS"];

if (typeof(OAA_CI_UTILS) == "undefined") {
  var OAA_CI_UTILS = {}; 
  OAA_CI_UTILS.result = null;
  OAA_CI_UTILS.log     = null;  
}

  /**
  * OpenAjax.a11y.console
  *
  * @desc
  * 
  * @param message 
  */

OAA_CI_UTILS.console = function (message) {
    var console = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
    console.logStringMessage(message);
    }  // end console

OAA_CI_UTILS.console("OAA_CI_UTILS loaded"); 


/** ================================================================
*
* updateToolbar
*
* @desc Extends standard toggleSidebar function by checking the toolbar button associated with the sidebar
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.updateToolbar = function(window, id) {

  // Reset the checked property of all toolbar buttons that create sidebars
  var button_elements           = window.document.getElementById("ID_TOOLBAR_BUTTON_ELEMENTS");  
  var button_lists           = window.document.getElementById("ID_TOOLBAR_BUTTON_LISTS");  
  var button_links              = window.document.getElementById("ID_TOOLBAR_BUTTON_LINKS");  
  var button_controls           = window.document.getElementById("ID_TOOLBAR_BUTTON_CONTROLS");  
  var button_tables             = window.document.getElementById("ID_TOOLBAR_BUTTON_TABLES");  
  var button_headings_landmarks = window.document.getElementById("ID_TOOLBAR_BUTTON_HEADINGS_LANDMARKS");  
  var button_equivalents        = window.document.getElementById("ID_TOOLBAR_BUTTON_EQUIVALENTS");  
  var button_color_contrast     = window.document.getElementById("ID_TOOLBAR_BUTTON_COLOR_CONTRAST");  

  button_elements.checked           = false;
  button_lists.checked           = false;
  button_links.checked              = false; 
  button_controls.checked           = false; 
  button_tables.checked             = false; 
  button_headings_landmarks.checked = false; 
  button_equivalents.checked        = false; 
  button_color_contrast.checked     = false;
  
  // Check the associated toolbar button
  
  switch (id) {
  case 'ID_VIEW_SIDEBAR_ELEMENTS':
    button_elements.checked = true;
    break;

  case 'ID_VIEW_SIDEBAR_LISTS':
    button_lists.checked = true;
    break;

  case 'ID_VIEW_SIDEBAR_LINKS':
    button_links.checked = true;
    break;

  case 'ID_VIEW_SIDEBAR_CONTROLS':
    button_controls.checked = true;
    break;

  case 'ID_VIEW_SIDEBAR_TABLES':
    button_tables.checked = true;
    break;


  case 'ID_VIEW_SIDEBAR_HEADINGS_LANDMARKS':
    button_headings_landmarks.checked = true;
    break;
  
  case 'ID_VIEW_SIDEBAR_EQUIVALENTS':
    button_equivalents.checked = true;
    break;
  
  case 'ID_VIEW_SIDEBAR_COLOR_CONTRAST':
    button_color_contrast.checked = true;
    break;
    
  }

}

/** ================================================================
*
* showWindowLog
*
* @desc shows the log information for the last evaluation
*
* @param  window   Object parent window for new window
* @param  format   String format of the the log file, valid values TEXT, XML, JSON and HTML
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showWindowLog = function(window, format) {

  if (OAA_CI_UTILS.oaa_log_window && 
    !OAA_CI_UTILS.oaa_log_window.closed) {
    OAA_CI_UTILS.showLogText();
  } 
  else {
    OAA_CI_UTILS.oaa_log_window = window.open("chrome://oaacacheinspector/content/oaa-log-window.xul", 
                      "oaaLogWindow", "chrome,centerscreen");
    OAA_CI_UTILS.oaa_log_window.onload = function () { OAA_CI_UTILS.showLogText(); };
  }

}


/** ================================================================
*
* showLogText
*
* @desc updates the log window with the text version of the log
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showLogText =function() {

  if (OAA_CI_UTILS.oaa_log_window && 
      OAA_CI_UTILS.oaa_log_window.document) {
 
    var oaa_log_textbox = OAA_CI_UTILS.oaa_log_window.document.getElementById('ID_OAA_LOG_TEXTBOX');                    
    
    if (oaa_log_textbox && OAA_CI_UTILS.log) {
      oaa_log_textbox.value = OAA_CI_UTILS.log.toString();
      
      OAA_CI_UTILS.checkLogToolbarButton('ID_OAA_LOG_BUTTON_TEXT');                    
      
    }    
  }      
}

/** ================================================================
*
* showLogXML
*
* @desc updates the log window with the text version of the log
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showLogXML =function() {

  if (OAA_CI_UTILS.oaa_log_window && 
      OAA_CI_UTILS.oaa_log_window.document) {
 
    var oaa_log_textbox = OAA_CI_UTILS.oaa_log_window.document.getElementById('ID_OAA_LOG_TEXTBOX');                    
    
    if (oaa_log_textbox && OAA_CI_UTILS.log) {
      oaa_log_textbox.value = OAA_CI_UTILS.log.toXML();
      
      OAA_CI_UTILS.checkLogToolbarButton('ID_OAA_LOG_BUTTON_XML');                    
      
    }
  }      
  
  
}

/** ================================================================
*
* showLogXML
*
* @desc updates the log window with the text version of the log
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showLogJSON =function() {

  if (OAA_CI_UTILS.oaa_log_window && 
      OAA_CI_UTILS.oaa_log_window.document) {
 
    var oaa_log_textbox = OAA_CI_UTILS.oaa_log_window.document.getElementById('ID_OAA_LOG_TEXTBOX');                    
    
    if (oaa_log_textbox && OAA_CI_UTILS.log) {
//      oaa_log_textbox.value = OAA_CI_UTILS.log.toJSON();
      
      OAA_CI_UTILS.checkLogToolbarButton('ID_OAA_LOG_BUTTON_JSON');                    
      
    }    
  }      
}

/** ================================================================
*
* checkLogToolbarButton
*
* @desc updates the log window with the text version of the log
*
* @param id String id of the button to check
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.checkLogToolbarButton = function( id ) {

  var button_text = OAA_CI_UTILS.oaa_log_window.document.getElementById('ID_OAA_LOG_BUTTON_TEXT');                    
  var button_xml  = OAA_CI_UTILS.oaa_log_window.document.getElementById('ID_OAA_LOG_BUTTON_XML');                    
  var button_json = OAA_CI_UTILS.oaa_log_window.document.getElementById('ID_OAA_LOG_BUTTON_JSON');                    

  if (button_text && button_xml && button_json) {
  
    button_text.checked = false;
    button_xml.checked = false;
    button_json.checked = false;

    switch (id) {
  
    case 'ID_OAA_LOG_BUTTON_TEXT':
      button_text.checked = true;  
      break;
  
    case 'ID_OAA_LOG_BUTTON_XML':
      button_xml.checked = true;  
      break;
  
    case 'ID_OAA_LOG_BUTTON_JSON':
      button_json.checked = true;  
      break;
    }  
  } 
}

/** ================================================================
*
* showWindowResults
*
* @desc shows the result information for the last evaluation
*
* @param  window   Object parent window for new window
* @param  format   String format of the the result file, valid values TEXT, XML, JSON and HTML
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showWindowResults = function(window, format) {

  if (OAA_CI_UTILS.oaa_result_window && 
    !OAA_CI_UTILS.oaa_result_window.closed) {
    OAA_CI_UTILS.showResultsText();
  } 
  else {
    OAA_CI_UTILS.oaa_result_window = window.open("chrome://oaacacheinspector/content/oaa-result-window.xul", 
                      "oaaResultsWindow", "chrome,centerscreen");
    OAA_CI_UTILS.oaa_result_window.onload = function () { OAA_CI_UTILS.showResultsText(); };
  }

}


/** ================================================================
*
* showResultsText
*
* @desc updates the result window with the text version of the result
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showResultsText =function() {

  if (OAA_CI_UTILS.oaa_result_window && 
      OAA_CI_UTILS.oaa_result_window.document) {
 
    var oaa_result_textbox = OAA_CI_UTILS.oaa_result_window.document.getElementById('ID_OAA_RESULTS_TEXTBOX');                    
    
    if (oaa_result_textbox && OAA_CI_UTILS.result) {
      var text = OAA_CI_UTILS.result.toString();
      oaa_result_textbox.maxLength = text.length;
      oaa_result_textbox.value = text;
      
      OAA_CI_UTILS.checkResultsToolbarButton('ID_OAA_RESULTS_BUTTON_TEXT');
      
    }    
  }      
}

/** ================================================================
*
* showResultsXML
*
* @desc updates the result window with the text version of the result
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showResultsXML =function() {

  if (OAA_CI_UTILS.oaa_result_window && 
      OAA_CI_UTILS.oaa_result_window.document) {
 
    var oaa_result_textbox = OAA_CI_UTILS.oaa_result_window.document.getElementById('ID_OAA_RESULTS_TEXTBOX');                    
    
    if (oaa_result_textbox && OAA_CI_UTILS.result) {
      var xml = OAA_CI_UTILS.result.toXML();
      oaa_result_textbox.maxLength = xml.length;
      oaa_result_textbox.value = xml;
      
      OAA_CI_UTILS.checkResultsToolbarButton('ID_OAA_RESULTS_BUTTON_XML');                    
      
    }
  }      
  
  
}

/** ================================================================
*
* showResultsXML
*
* @desc updates the result window with the text version of the result
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.showResultsJSON =function() {

  if (OAA_CI_UTILS.oaa_result_window && 
      OAA_CI_UTILS.oaa_result_window.document) {
 
    var oaa_result_textbox = OAA_CI_UTILS.oaa_result_window.document.getElementById('ID_OAA_RESULTS_TEXTBOX');                    
    
    if (oaa_result_textbox && OAA_CI_UTILS.result) {
      var json = OAA_CI_UTILS.result.toJSON();
      oaa_result_textbox.maxLength = json.length;
      oaa_result_textbox.value = json;
      
      OAA_CI_UTILS.checkResultsToolbarButton('ID_OAA_RESULTS_BUTTON_JSON');                    
      
    }    
  }      
}

/** ================================================================
*
* checkResultsToolbarButton
*
* @desc updates the result window with the text version of the result
*
* @param id String id of the button to check
*
* @return nothing
*
* =============================================================== */

OAA_CI_UTILS.checkResultsToolbarButton = function( id ) {

  var button_text = OAA_CI_UTILS.oaa_result_window.document.getElementById('ID_OAA_RESULTS_BUTTON_TEXT');
  var button_xml  = OAA_CI_UTILS.oaa_result_window.document.getElementById('ID_OAA_RESULTS_BUTTON_XML');
  var button_json = OAA_CI_UTILS.oaa_result_window.document.getElementById('ID_OAA_RESULTS_BUTTON_JSON');

  if (button_text && button_xml && button_json) {
  
    button_text.checked = false;
    button_xml.checked  = false;
    button_json.checked = false;

    switch (id) {
  
    case 'ID_OAA_RESULTS_BUTTON_TEXT':
      button_text.checked = true;  
      break;
  
    case 'ID_OAA_RESULTS_BUTTON_XML':
      button_xml.checked = true;  
      break;
  
    case 'ID_OAA_RESULTS_BUTTON_JSON':
      button_json.checked = true;  
      break;
    }  
  } 
}